package com.zbkj.service.dividend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.service.dividend.dao.DividendPoolRecordMapper;
import com.zbkj.service.dividend.model.DividendPoolRecord;
import com.zbkj.service.dividend.request.DividendPoolRecordRequest;
import com.zbkj.service.dividend.service.DividendPoolRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户分红记录服务实现类
 */
@Service
public class DividendPoolPoolRecordServiceImpl extends ServiceImpl<DividendPoolRecordMapper, DividendPoolRecord> implements DividendPoolRecordService {

    @Resource
    private DividendPoolRecordMapper dividendPoolRecordMapper;

    /**
     * 分页查询分红记录
     */
    @Override
    public List<DividendPoolRecord> getList(DividendPoolRecordRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        return dividendPoolRecordMapper.findPageList(request);
    }

    /**
     * 根据用户ID查询分红记录
     */
    @Override
    public List<DividendPoolRecord> getByUserId(Integer userId) {
        return dividendPoolRecordMapper.findByUserId(userId);
    }

    /**
     * 根据订单No查询分红记录
     */
    @Override
    public DividendPoolRecord getByOrderNo(Integer orderNo) {
        return dividendPoolRecordMapper.findByOrderNo(orderNo);
    }

    /**
     * 统计用户分红总额
     */
    @Override
    public BigDecimal getTotalDividendByUserId(Integer userId) {
        return dividendPoolRecordMapper.sumDividendByUserId(userId);
    }

    /**
     * 创建分红记录
     */
    @Override
    public int createDividendRecord(DividendPoolRecord record) {
        record.setCreateTime(LocalDateTime.now());
        return dividendPoolRecordMapper.insert(record);
    }

    @Override
    public int batchCreateDividendRecords(List<DividendPoolRecord> records) {
        return dividendPoolRecordMapper.batchInsert(records);
    }

    /**
     * 获取分红统计信息
     */
    @Override
    public List<DividendPoolRecord> getDividendStats(String startDate, String endDate) {
        return dividendPoolRecordMapper.getDividendStats(startDate, endDate);
    }
}